﻿# encoding: utf-8
########################### 读书 ########################################
require "iconv"
require_relative "create_cell"

utf_8_gb2312= Iconv.new("gb2312", "utf-8") # utf-8 转为 gb2312
gb2312_utf_8= Iconv.new("utf-8", "gb2312") # gb2312 转为 utf-8

sentence=""
io_r=File.open("book.txt","r")
while  tem= io_r.getc
	print tem.encoding.name+"  "
	print tem
	puts
	if tem == utf_8_gb2312.iconv("。")
		break
	end
	sentence.concat(tem)
end                                                #####         <<===########
io_r.close
#n=0
#@f  ;  @a
#len=sentence.size
#if h.key?(sentence[n])
#	@f=$cell_in_hash[sentence].friend
#	@a=$cell_in_hash[sentence].attribute ;
#       @f.each {|elem|
#	    if  /^#{elem}/ =~ sentence[(n+1)...len]
#	    break
#	    end
hw="/ "
io_r=open("words.txt","r")
while tem=io_r.gets
	print tem.encoding.name+"  "
	puts tem
	tem.chomp!
	hw.concat("|")
	hw.concat(tem)
end
hw.concat(" /")
puts hw
arr1=[]; n=0        ###===>> 只需一次匹配，并把匹配成功片段存进数组
str1=sentence.gsub(eval(hw)){|matched|
	arr1[n]=matched    #存储成功匹配部分
	n+=1
	puts matched
	matched="@"
}         ###              <<===####

#####===>>  读取属性值，并挂起，等待指令

#####		<<===#####

str2=str1.gsub(/@+/,"@")   ### 取得字符串用来创建 cell 对象
str2_2=str2              # 为未匹配成功部分保留一个带有@的副本
arr3=str2.split(/@/)       # 以@为界分割字符串 ，得到结果为一个数组(未成功匹配部分)

arr3.each{|elem|           ######===>> 创建cell 对象
	create_cell(elem)
}                          ####          <<===######

n1=0                       ##################===>> 还原目标句子
arr4=arr3.each{|elem|     # 遍历未成功匹配部分
	if elem== "@"
		elem=str1[n1]
		n1+=1
	end
}                          #####         <<===#################
p arr3
